53. 最大子数组和
为保证权益,题目请参考 53. 最大子数组和(From LeetCode).
解决方案1
CPP
C++
/*
* LeetCode 53
*
* Date: 2020-05-03
* Author: Keven Ge
*/
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <limits.h>
using namespace std;
class Solution {
public:
int maxSubArray(vector<int> &nums) {
int max_sum = INT_MIN; // the result for the function.
int cur_sum = 0; // the current sum for the function.
for (int num:nums) {
if(cur_sum >=0){
cur_sum += num;
}else{
cur_sum = num;
}
max_sum = max(max_sum, cur_sum);
}
return max_sum;
}
};
int main() {
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36